package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
        int ans = 0;
        for(int i = leftBorder;i <= rightBorder;i++){
            ans += i % 2 == 0 ? i : 0;
        }
        return ans;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int ans = 0;
        for(int i = leftBorder;i <= rightBorder;i++){
            ans += i % 2 == 1 ? i : 0;
        }
        return ans;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        int ans = 0;
        for (Integer integer : arrayList) {
            ans += integer * 3 + 2;
        }


        return ans;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (Integer integer : arrayList) {
            list.add(integer * 3 + 2);
        }
        return list;

    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        int ans = 0;
        for (Integer integer : arrayList) {
            ans += integer * 3 + 5;
        }
        return ans;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i) % 2 == 0) list.add(arrayList.get(i));
        }
        double ans = 0;
        if(arrayList.size() % 2 == 0) {
            ans =( 1.0*arrayList.get(arrayList.get(arrayList.size()/2)) + arrayList.get(arrayList.get(arrayList.size()/2-1)) ) / 2;
        } else {
            ans = arrayList.get(arrayList.size()/2);
        }
        return ans;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        double ans = 0.0;
        int num = 0;
        for (Integer integer : arrayList) {
            if(integer % 2 == 0){
                ans += integer;
                num++;
            }
        }
        return ans / num;
    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        if(specialElment % 2 == 1) return false;
        for (Integer integer : arrayList) {
            if(integer % 2 == 0 && integer == specialElment){
                return true;
            }
        }
        return false;
    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        Set<Integer> set = new HashSet<>();
        for (Integer integer : arrayList) {
            if(integer % 2 == 0)set.add(integer);
        }
        return new ArrayList<>(set);
    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        LinkedList<Integer> list1 = new LinkedList<>();
        LinkedList<Integer> list2 = new LinkedList<>();
        for (Integer integer : arrayList) {
            if(integer % 2 == 0){
                list1.add(integer);
            }else{
                list2.addFirst(integer);
            }
        }
        list1.addAll(list2);
        return list1;
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {
        List<Integer> ans = new ArrayList<>();
        for (int i = 1; i < arrayList.size(); i++) {
            ans.add((arrayList.get(i-1) + arrayList.get(i))*3);
        }
        return ans;
    }
}
